package com.bruce.learnrecylerview.app.ui.adapter;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import com.bruce.learnrecycler.kotlin.ui.listitem.Item;
import com.bruce.learnrecylerview.app.R;
import com.bruce.learnrecylerview.app.action.ActionDelegate;
import com.bruce.learnrecylerview.app.ui.adapter.viewholder.BaseViewHolder;
import com.bruce.learnrecylerview.app.ui.adapter.viewholder.TimerViewHolder;

import javax.inject.Inject;
import java.util.List;

/**
 * Created by wangzhijie@wind-mobi.com on 2015/9/1.
 */
@Deprecated
public class MainAdapter extends RecyclerView.Adapter<BaseViewHolder> {
    private List<Item> items;
    private LayoutInflater inflater;
    private ActionDelegate delegate;

    @Inject
    public MainAdapter(LayoutInflater inflater, ActionDelegate delegate) {
        this.inflater = inflater;
        this.delegate = delegate;
    }

    public List<Item> getItems() {
        return items;
    }

    public void setItems(List<Item> items) {
        this.items = items;
        //notifyDataSetChanged();
    }

    /**
     * Called when RecyclerView needs a new {@link ViewHolder} of the given type to represent
     * an item.
     * <p>
     * This new ViewHolder should be constructed with a new View that can represent the items
     * of the given type. You can either create a new View manually or inflate it from an XML
     * layout file.
     * <p>
     * The new ViewHolder will be used to display items of the adapter using
     * {@link #onBindViewHolder(ViewHolder, int)}. Since it will be re-used to display different
     * items in the data set, it is a good idea to cache references to sub views of the View to
     * avoid unnecessary {@link View#findViewById(int)} calls.
     *
     * @param parent   The ViewGroup into which the new View will be added after it is bound to
     *                 an adapter position.
     * @param viewType The view type of the new View.
     * @return A new ViewHolder that holds a View of the given view type.
     * @see #getItemViewType(int)
     * @see #onBindViewHolder(ViewHolder, int)
     */
    @Override
    public BaseViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == R.layout.base_item) {
            return new BaseViewHolder(inflater.inflate(viewType, parent, false));
        }
        return new TimerViewHolder(inflater.inflate(viewType, parent, false));
    }

    /**
     * Called by RecyclerView to display the data at the specified position. This method
     * should update the contents of the {@link ViewHolder#itemView} to reflect the item at
     * the given position.
     * <p>
     * Note that unlike {@link ListView}, RecyclerView will not call this
     * method again if the position of the item changes in the data set unless the item itself
     * is invalidated or the new position cannot be determined. For this reason, you should only
     * use the <code>position</code> parameter while acquiring the related data item inside this
     * method and should not keep a copy of it. If you need the position of an item later on
     * (e.g. in a click listener), use {@link ViewHolder#getAdapterPosition()} which will have
     * the updated adapter position.
     *
     * @param holder   The ViewHolder which should be updated to represent the contents of the
     *                 item at the given position in the data set.
     * @param position The position of the item within the adapter's data set.
     */
    @Override
    public void onBindViewHolder(BaseViewHolder holder, int position) {
        // todo
        holder.tvMain.setText(items.get(position).getName());
        if (holder instanceof  TimerViewHolder) {
            TimerViewHolder timerViewHolder = (TimerViewHolder) holder;
            timerViewHolder.tvTimer.setText(items.get(position).getTime());
        }

        holder.itemView.setOnClickListener(view -> delegate.requestByLineNameStationName("166", "桂林路田林路", true, holder.getAdapterPosition()));
    }

    /**
     * Returns the total number of items in the data set hold by the adapter.
     *
     * @return The total number of items in this adapter.
     */
    @Override
    public int getItemCount() {
        return items == null ? 0:items.size();
    }

    @Override
    public int getItemViewType(int position) {
        return items == null? 0 :items.get(position).getType();
    }
}
